import {
  Controller,
  Delete,
  Query,
  Get,
  UseInterceptors,
  Req,
} from '@nestjs/common';
import { Result } from '../../common/result/Result';
import { AuthService } from './auth.service';
import { AuthInterceptor } from '../../config/filter/auth.interceptor';

@Controller('/api/v1/auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  /**
   * 登录接口
   * @param username
   * @param password
   * @param captchaKey
   * @param captchaCode
   */
  @Get('/login')
  async login(
    @Query('username') username: string,
    @Query('password') password: string,
    @Query('captchaKey') captchaKey: string,
    @Query('captchaCode') captchaCode: string,
  ): Promise<Result<any>> {
    const result = await this.authService.login(
      username,
      password,
      captchaKey,
      captchaCode,
    );
    return result;
  }

  /**
   * 登出
   * @param req
   */
  @Delete('/logout')
  @UseInterceptors(AuthInterceptor)
  async logout(@Req() req) {
    await this.authService.logout(req.user);
    return Result.success();
  }

  /**
   * 验证码信息获取
   */
  @Get('/captcha')
  async getCaptcha(): Promise<Result<any>> {
    const result = await this.authService.getCaptcha();
    return Result.success(result);
  }
}
